<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/12/31 0031
 * Time: 15:48
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);


namespace app\common\business;

use app\admin\model\SystemConfig;
use app\common\model\MallAccountLog as ModelObj;
use app\common\model\MallMember;
use app\common\model\MallWithdrawal;
use think\Exception;

class Member extends Base
{
    /**
     * @var ModelObj
     */
    protected ModelObj $modelObj;

    /**
     *
     */
    public function __construct()
    {
        $this->modelObj = new ModelObj();
    }

    /**
     * 记录钱包明细
     * @param string $openid
     * @param float $money
     * @param int $changType
     * @param string $changedesc
     * @return bool
     */
    public function saveAccoutLog(string $openid,float $money,int $changType=1,string $changedesc='商城消费'){

        $member =(new MallMember())->getOpenidById($openid);
        // 3 分享  4平台增加
        if($changType>2){
            $balance =round($member['balance']+$money,2);
        }else{ // 0 平台减少 1余额支付 2提现
            $balance =round($member['balance']-$money,2);
        }

        if($balance < 0){
            throw new Exception('余额不足');
        }
        $data=array(
            'money' =>$money,
            'openid'=>$openid,
            'mid'  =>$member['id'],
            'balance'=>$balance,
            'change_type'=>$changType,
            'change_date'=>date('Y-m-d'),
            'change_desc'=>$changedesc
        );
        try{
            $this->modelObj->save($data);

            $member->save(array('balance'=>$balance));
        }catch (\Exception $e){
            throw new Exception($e->getMessage());
        }
        return true;
    }

    /**
     * 获取用户钱包明细
     * @param $openid
     * @param $type
     * @param $page
     * @return mixed
     */
    public function getAccountLog($openid,$type,$page,$start,$end){
        $where =[];
        if($type ==1){
            $where[] =[['change_type','in',[0,1,2]]];
        }else{
            $where[] =[['change_type','in',[3,4]]];
        }
        if(!$start || !$end){
            $where[] =[['change_date','=',date('Y-m-d')]];
        }else{
            $where[] =[['create_time','>',strtotime($start)],['create_time','<',strtotime($end)]];
        }
        $where[]=[['openid','=',$openid]];
        try{
           $list= $this->modelObj->field('id,money,balance,change_type,change_desc,create_time')->where($where)->order('create_time desc')->page($page,10)->select()->toArray();
        }catch (\Exception $e){
            throw new Exception($e->getMessage());
        }
        if($list){
            foreach ($list as $k=>$v){
                $list[$k]['create_time'] =date('m月d日 H:i',strtotime($v['create_time']));
            }
        }

        return $list;
    }

    /**
     * 获取用户最近一次提现
     * @param $openid
     * @return mixed
     */
    public function getWithdrawal($openid){
        try{
           $info= (new MallWithdrawal())->where([['openid','=',$openid]])->order('id desc')->find();
        }catch (\Exception $e){
            throw new Exception($e->getMessage());
        }
        return $info;
    }

    /**
     * 写入提现记录表
     * @param $data
     * @return bool
     */
    public function saveWithdrawal($data){
        try{
            $info= (new MallWithdrawal())->save($data);
        }catch (\Exception $e){
            throw new Exception($e->getMessage());
        }
        return $info;
    }

    /**
     * 获取商城配置信息
     * @return SystemConfig[]|array|\think\Collection
     * @throws Exception
     */
    public function getSystenConfig($name){

        try{
            $site = (new SystemConfig())->field('id,value')->where([['group','=','site'],['name','=',$name]])->find();
        }catch (\Exception $e){
            throw new Exception($e->getMessage());
        }
        return $site['value'];
    }
}